<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/12
 * Time: 11:31
 */

namespace app\index\controller;

use app\index\model\Login as LoginModel;
use app\lib\exception\ErrorMessage;
use app\lib\exception\SuccessMessage;
use think\Controller;
use think\Session;

class Login extends Controller
{
    public function index()
    {
        return $this->fetch();
    }

    public function userLogin()
    {
        $data['mobile'] = input('post.mobile');
        $data['password'] = md5(md5(input('post.password')));

        $userLogin = new LoginModel();
        $info = $userLogin->vailateUserInfo($data);

        if($info){
            session('id', $info['id']);         //用户ID
            session('username', $info['username']);  //用户名
            session('pic', $info['pic']); //用户头像
            session('code', $info['code']);    //推广码
            session('mobile', $info['mobile']);        //手机号

            return json(new SuccessMessage(), 201);
        }else{
            return json(new ErrorMessage(),202);
        }

    }

    //退出
    public function loginOut()
    {
        Session::clear();
        return $this->fetch('index');
    }
}